package networkListener;

import org.pcap4j.packet.*;
import java.net.InetAddress;

public class PacketProcessor {

    private final Packet packet;

    public PacketProcessor(Packet packet) {
        this.packet = packet;
    }

    public String[] processPacket() {
        // 解析以太网帧
        EthernetPacket ethernetPacket = packet.get(EthernetPacket.class);
        if (ethernetPacket == null) {
            return null;  // 如果不是以太网帧则跳过
        }

        String srcMac = ethernetPacket.getHeader().getSrcAddr().toString();
        String dstMac = ethernetPacket.getHeader().getDstAddr().toString();

        // 获取IP数据包
        IpV4Packet ipV4Packet = packet.get(IpV4Packet.class);
        if (ipV4Packet == null) {
            return null;  // 如果没有IP数据包则跳过
        }

        InetAddress srcIp = ipV4Packet.getHeader().getSrcAddr();
        InetAddress dstIp = ipV4Packet.getHeader().getDstAddr();

        // 返回处理后的数据
        return new String[]{
                srcMac,
                srcIp.getHostAddress(),
                dstMac,
                dstIp.getHostAddress(),
                String.valueOf(packet.length())
        };
    }
}
